package main

import (
	"whoscored/util/mysql"
	"fmt"
)

func winOrLoss(v map[string]interface{},isHome string ) (int64,int64,int64) {
	winMap:=mysql.FindFirst("select count(*) as count from team_match where teamId=? and regionCode=? and season=? and tournamentName=?" +
		" and homeScore>awayScore and isHome="+isHome,
		v["teamId"],v["regionCode"],v["season"],v["tournamentName"],
	)
	win:=winMap["count"]
	drawMap:=mysql.FindFirst("select count(*) as count from team_match where teamId=? and regionCode=? and season=? and tournamentName=?"+
		" and homeScore=awayScore and isHome="+isHome,
		v["teamId"],v["regionCode"],v["season"],v["tournamentName"],
	)
	draw:=drawMap["count"]
	lossMap:=mysql.FindFirst("select count(*) as count from team_match where teamId=? and regionCode=? and season=? and tournamentName=?"+
		" and homeScore<awayScore and isHome="+isHome,
		v["teamId"],v["regionCode"],v["season"],v["tournamentName"],
	)
	loss:=lossMap["count"]
	return win.(int64),draw.(int64),loss.(int64)
}
func main()  {
	seasonList:=mysql.FindAll("select * from team_season")

	for _,v:=range seasonList {
		win1,draw1,loss1:=winOrLoss(v,"1")
		loss2,draw2,win2:=winOrLoss(v,"0")
		mysql.UpdateOrDelete("update team_season set homeWin=?,homeDraw=?,homeLoss=?,awayWin=?,awayDraw=?,awayLoss=? " +
			"where id="+v["id"].(string),
			win1,draw1,loss1,win2,draw2,loss2,
		)
		fmt.Println(v["id"])
	}
}